<?php

namespace Admin\Controller;

class WithdrawController extends AdminbaseController {
    
    protected $withdrawModel;
    protected $accountDetailModel;
    protected $ladderModel;
            
    function _initialize() {
        parent::_initialize();
        $this->withdrawModel = D('withdraw');
        $this->accountDetailModel = D('accountDetail');
        $this->ladderModel = D('ladder');
    }
    
    public function index() {

        $startTime = $_REQUEST['startTime'];
        $endTime = $_REQUEST['endTime'];
        $name = $_REQUEST['name'];
        $status = $_REQUEST['status'];
        
        $where = '1=1';
        
        if(!isNullOrEmpty($startTime)) {
            $where .= ' and w.create_time >= "' .toSqlStartTime($startTime) .'"';
            $this->assign('startTime', $startTime);
            $parameter['startTime'] = $startTime;
        } 
        
        if(!isNullOrEmpty($endTime)) {
            $where .= ' and w.create_time <= "' .toSqlStartTime($endTime) .'"';
            $this->assign('endTime', $endTime);
            $parameter['endTime'] = $endTime;
        }
        
        if(!isNullOrEmpty($name)) {
            $where .= ' and u.user_name like "%' .$name.'%"';
            $this->assign('name', $name);
            $parameter['name'] = $name;
        }
        
        if(!isNullOrEmpty($status)) {
            $where .= ' and w.status = ' . $status;
            $this->assign('status', $status);
            $parameter['status'] = $status;
        }
        
        $count = $this->withdrawModel->alias('w')
                ->join('left join l_ladder l on l.ladder_id = w.ladder_id')
                ->join('left join l_user u on u.user_id = l.user_id')
                ->field('w.*,u.user_name,l.alipay_account,l.wechat_account,l.bank_account')
                ->where($where)
                ->count();
        $page = $this->page($count,20);

        $list = $this->withdrawModel->alias('w')
                ->join('left join l_ladder l on l.ladder_id = w.ladder_id')
                ->join('left join l_user u on u.user_id = l.user_id')
                ->field('w.*,u.user_name,l.alipay_account,l.wechat_account,l.bank_account')
                ->where($where)
                ->limit($page->firstRow . ',' . $page->listRows)->select();
        
        $this->assign('statusEnum', C('WITHDRAW_STATUS'));
        $this->assign('typeEnum', C('WITHDRAW_TYPE'));
        $this->assign("page", $page->show('Admin', $parameter));
        $this->assign("list", $list);
        $this->display();
    }

    public function endWithdraw() {
        
        $id = I('get.id');
        
        $withdraw = $this->withdrawModel->where(array('withdraw_id'=>$id))->find();
        
        $withdraw['end_time'] = getCurrentDate();
        $withdraw['status'] = C('WITHDRAW_STATUS.END');
        
        $this->withdrawModel->where(array('withdraw_id'=>$id))->save($withdraw);
        
        //更新账户流水
        $accountDetail['detail_type'] = C('ACCOUNT_DETAIL_TYPE.WITHDRAW');
        $accountDetail['ladder_id'] = $withdraw['ladder_id'];
        $accountDetail['money'] = $withdraw['money'];
        $accountDetail['create_time'] = getCurrentDate();
        $accountDetail['withdraw_id'] = $withdraw['withdraw_id'];
        
        $existAccountDetail = $this->accountDetailModel->where(array('withdraw_id'=>$withdraw['withdraw_id']))->find();
        
        if(isNullOrEmpty($existAccountDetail)) {
            
            $this->accountDetailModel->add($accountDetail);
            
            //更新总账户
            $ladder = $this->ladderModel->where(array('ladder_id'=>$withdraw['ladder_id']))->find();
            
            $ladder['total_assets'] = $ladder['total_assets'] - $withdraw['money'];
            
            $this->ladderModel->where(array('ladder_id'=>$ladder['ladder_id']))->save($ladder);
            
        }
        
        $this->success('处理成功', U('withdraw/index'));
        
    }
    
}